package com.example.javaee_library.Service.Impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.example.javaee_library.Entity.Appointment;
import com.example.javaee_library.Entity.Book;
import com.example.javaee_library.Service.Book_Service;
import com.example.javaee_library.dao.AppointmentDao;
import com.example.javaee_library.dao.BookDao;
import com.example.javaee_library.dto.AppointExecution;

import com.example.javaee_library.enums.AppointStateEnum;
import com.example.javaee_library.exception.AppointException;
import com.example.javaee_library.exception.NoBookException;
import com.example.javaee_library.exception.NoNumberException;
import com.example.javaee_library.exception.RepeatAppointException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author Miao-yongzheng
 * @create 2021-12-10 11:36
 */
@Slf4j
@Service
public class Book_ServiceImpl implements Book_Service {
    @Autowired
    BookDao bookDao;
    @Autowired
    AppointmentDao appointmentDao;
    @Override
    public Book getById(long bookId) {
        return bookDao.queryById(Convert.toInt(bookId));
    }

    @Override
    public List<Book> getList() {
        return bookDao.queryAll(0, 1000);
    }

    @Override
    @Transactional
    public AppointExecution appoint(long bookId, long studentId) {
        try {
            // 减库存
            int update = bookDao.reduceNumber(bookId);
            if (update <= 0) {// 库存不足
                throw new NoNumberException("库存不足");
            } else {
                // 执行预约操作
                int log = appointmentDao.Insertaction(bookId,studentId,"借书");
                int insert = appointmentDao.insertAppointment(bookId, studentId);
                if (insert <= 0) {// 重复预约
                    throw new RepeatAppointException("重复借阅");
                } else {// 预约成功
                    Appointment appointment = appointmentDao.queryByKeyWithBook(bookId, studentId);
                    return new AppointExecution(bookId, AppointStateEnum.SUCCESS, appointment);
                }
            }
        } catch (NoNumberException e1) {
            throw e1;
        } catch (RepeatAppointException e2) {
            throw e2;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // 所有编译期异常转换为运行期异常
            throw new AppointException("appoint inner error:" + e.getMessage());
        }
    }


    @Override
    public boolean AddBook(int bookId, String bookName, int bookNum) {
        if(bookDao.queryById(bookId)!=null)
        {
            throw new RepeatAppointException(505, "有此书,不能插入");
        }
        else
        return bookDao.Insert(bookId,bookName,bookNum)>0;
    }

    @Override
    public boolean Updatebook(int bookId, String bookName, int bookNum) {
        if(bookDao.queryById(bookId)==null)
        {
            throw new NoBookException(506,"无此书不能修改");
        }
        else
            return bookDao.Update(bookId,bookName,bookNum)>0;
    }

    @Override
    public List<Book> Serach(String bookName) {
        System.out.println("查询了"+'%'+bookName+'%');
        List<Book> result = bookDao.Serach('%'+bookName+'%');
        return  result;
    }

    @Override
    public int appoint_again(long bookId, long studentId, Timestamp timestamp) {
       return appointmentDao.Appoint_again(bookId,studentId,timestamp);
    }


    /*  @Override
      public int appoint_again(long bookId, long studentId) {
         *//* try {
            // 减库存

                // 执行预约操作

                    int back = appointmentDao.
                    return new AppointExecution(bookId, AppointStateEnum.SUCCESS, appointment);


        } catch (NoNumberException e1) {
            throw e1;
        } catch (RepeatAppointException e2) {
            throw e2;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // 所有编译期异常转换为运行期异常
            throw new AppointException("appoint inner error:" + e.getMessage());
        }*//*
    }
*/
    @Transactional
    @Override
    public boolean returnBook(int bookId, int studentId) {
        Appointment appointExecution = appointmentDao.queryByKeyWithBook(bookId,studentId);
        Date date  = appointExecution.getAppoint_Time();
        Date now   = new Date();
        long betweenDay = DateUtil.between(date,now,DateUnit.DAY);
        //计费标准为一天0.2元,大于14天就计费
        if(betweenDay>14)
        {
            int money = (int) ((betweenDay-14)*0.2);
            throw new AppointException(999,"还书失败,逾期大于14天需要还"+money);
        }
        else
        {
            appointmentDao.Insertaction(bookId,studentId,"还书");
            if(bookDao.deleteAppiont(bookId,studentId)!=0){
                bookDao.AddNumber1(bookId);
            }
            else
                throw new AppointException(989,"还书失败未知错误");
        }
        return true;
    }
}
